<template>
<div class="main_box" style="padding:10px">
  <div class="add_wrap">
    <div class="tit">
      <h2>基础信息</h2>
      <el-button type="success" plain size="small" icon="fa fa-mail-forward" @click="returnList">返回</el-button>
    </div>
    <el-form :model="addForm" ref="addForm" label-width="200px" :rules="roleRules">
      <el-row>
        <!--<el-form-item label="菜单类型：" prop="menuType">
          <span v-if="addForm.menuType==0">菜单分组</span>
        </el-form-item>-->
        <el-form-item label="上级菜单：">
          <span>{{parentMenuName}}</span>
        </el-form-item>
        <el-form-item label="菜单类别：" prop="menuCatetory">
          <el-select v-model="addForm.menuCatetory" @change="catetoryChange" placeholder="请选择菜单类别">
            <el-option key="1" label="系统" value="1"></el-option>
            <el-option key="2" label="H5" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单名称：" prop="menuName">
          <el-input v-model="addForm.menuName" @keydown.native="keydown($event)" :clearable="true"></el-input>
        </el-form-item>
        <el-form-item label="菜单编码：" prop="menuCode">
          <el-input v-model="addForm.menuCode" @keydown.native="keydown($event)"></el-input>
        </el-form-item>
        <el-form-item label="资源路径：" prop="menuUrl">
          <el-input v-model="addForm.menuUrl" :clearable="true"></el-input>
        </el-form-item>
        <el-form-item label="排序(正序)：" prop="sortby">
          <el-input v-model="addForm.sortby" :clearable="true"></el-input>
        </el-form-item>
        <el-form-item label="描述：" prop="descriptions">
          <el-input type="textarea" v-model="addForm.descriptions" :clearable="true"></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item>
          <el-button plain type="primary" @click="sureClick">保 存</el-button>
          <el-button @click="returnList">取 消</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </div>
 </div>
</template>

<script>
import * as message from "@/constants/message"
import AuthApi from "@/api/system/auth";

export default{
  data(){
      return {
        addForm:{
          parentId: "",
          menuName: "",
          menuType: "",
          menuCatetory: "",
          menuCode: "",
          menuUrl: "",
          sortby: "",
          descriptions: ""
        },
        parentMenuName: "",
        menuTypes:[
          { label:"显示", value:1},
          { label:"不显示", value:0}
        ],
        roleRules: {
          menuName: [
            { required: true, message: "请输入菜单名称", trigger: "blur" }
          ],
          menuType: [
            { required: true, message: "请选择菜单类型", trigger: "change" }
          ],
          menuCode: [
            { required: true, message: "请输入菜单编码", trigger: "blur" }
          ]/*,
          menuUrl:[
            { required: true, message: "请输入资源路径", trigger: "blur" }
          ]*/
        }
      }
  },
  components: {
  },
  mounted(){
    this.init();
  },
  methods: {
    init(){
      this.getData();
    },
    keydown(e) {
      if(e.keyCode== 32){
        e.returnValue=false;
      }
    },
    getData(){
      let _this = this
      let parentId = _this.$route.query.id
      if (parentId == 0){
        _this.initForm()
      }else{
        AuthApi.getMenuInfo({menuId:parentId})
          .then(res => {
            _this.addForm.parentId = parentId
            _this.addForm.menuCatetory = res.data.menuCatetory.toString()
            _this.addForm.menuCode = res.data.menuCode
            _this.addForm.menuType = Number(res.data.menuType)+1
            _this.parentMenuName = res.data.menuName
          })
      }
    },
    initForm(){
      this.addForm = {
        parentId: "0",
        menuName: "",
        menuType: "0",
        menuCatetory: "1",
        menuCode: "SYSTEM_MANAGER_",
        menuUrl: "",
        descriptions: ""
      }
      this.parentMenuName = "无"
    },
    catetoryChange(val){
      if(val == 1){
        this.addForm.menuCode = "SYSTEM_MANAGER_";
      }else if(val == 2){
        this.addForm.menuCode = "SYSTEM_HTML_";
      }
    },
    sureClick() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          this.$confirm(message.MSG_CONFIRM_SAVE, message.TITLE_PROMPT, {})
            .then(() => {
                this.addMenu();
          });
        }
      })
    },
    addMenu() {
      let data = {
        menuName: this.addForm.menuName,
        menuType: this.addForm.menuType,
        menuCatetory: this.addForm.menuCatetory,
        menuCode: this.addForm.menuCode,
        menuUrl: this.addForm.menuUrl,
        sortby: this.addForm.sortby,
        descriptions: this.addForm.descriptions,
        parentId: this.addForm.parentId
      };
      AuthApi.addMenu(data).then(data => {
          this.$Utils.notifySuccess();
          this.returnList();
      })
    },
    returnList(){
      this.$store.dispatch('delView', this.$route);
      this.$router.push({path: '/sys/menuManagement'});
    }
  }
}
</script>
<style lang="scss" scoped>
@import "@/assets/css/views/add.scss";
</style>
